package com.sylar.lelepub;

import java.util.ArrayList;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

public class TabSearchActivity extends ActivityGroup implements OnClickListener {
	private View btn_title_right;
	private TextView tv_title_name;
	private CheckBox cbox_short;
	private TextView tv_search_city_name;
	private TextView tv_search_time;
	private TextView tv_search_night;
	private TextView tv_search_loc;
	private ArrayList<View> history;
	private View CityView;
	private View MainView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.history = new ArrayList<View>();

		MainView = getLayoutInflater().inflate(R.layout.layout_search, null);

		findView();

		setData();

		replaceView(MainView);

	}

	void findView() {

		if (MainView == null)
			return;

		tv_title_name = (TextView) MainView.findViewById(R.id.tv_title_name);
		btn_title_right = MainView.findViewById(R.id.btn_title_right);
		btn_title_right.setVisibility(View.GONE);

		View ly_city = MainView.findViewById(R.id.ly_city);
		View ly_time = MainView.findViewById(R.id.ly_time);
		View ly_night = MainView.findViewById(R.id.ly_night);
		View ly_loc = MainView.findViewById(R.id.ly_loc);

		ly_city.setOnClickListener(this);
		ly_time.setOnClickListener(this);
		ly_night.setOnClickListener(this);
		ly_loc.setOnClickListener(this);

		tv_search_city_name = (TextView) MainView.findViewById(R.id.tv_search_city_name);
		tv_search_time = (TextView) MainView.findViewById(R.id.tv_search_time);
		tv_search_night = (TextView) MainView.findViewById(R.id.tv_search_night);
		tv_search_loc = (TextView) MainView.findViewById(R.id.tv_search_loc);

		cbox_short = (CheckBox) MainView.findViewById(R.id.cbox_short);

		Button btn_do_search = (Button) MainView.findViewById(R.id.btn_do_search);
		btn_do_search.setOnClickListener(this);

	}

	void setData() {
		if (tv_title_name != null)
			tv_title_name.setText("酒店搜索");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ly_city:
			doChangeCity();
			break;
		case R.id.ly_time:
			doChangeTime();
			break;
		case R.id.ly_night:
			doChangeNight();
			break;
		case R.id.ly_loc:
			doChangeRegion();
			break;
		case R.id.btn_do_search:
			doSearch();
			break;

		}
	}

	void doChangeCity() {
		Intent intent = new Intent(this, SearchCityActivity.class);
		CityView = getLocalActivityManager().startActivity("activity1", intent).getDecorView();
		View btn_title_left = CityView.findViewById(R.id.btn_title_left);
		btn_title_left.setVisibility(View.VISIBLE);
		btn_title_left.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				back();
			}
		});

		replaceView(CityView);

	}

	void doChangeTime() {

	}

	void doChangeNight() {

	}

	void doChangeRegion() {

	}

	void doSearch() {
		Intent intent = new Intent(this,TabNearActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);
		
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			back();
			break;
		}
		return true;
	}

	public void replaceView(View v) {
		history.add(v);
		setContentView(v);
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
				back();
			}
			return true;
		} else {
			return super.dispatchKeyEvent(event);
		}

	}

	public void back() {
		if (history.size() > 1) {
			history.remove(history.size() - 1);
			View v = history.get(history.size() - 1);
			setContentView(v);
		} else {
			finish();
		}
	}

}
